package com.zhilianbao.leyaogo.ui.adapter.home.magic;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bql.utils.CheckUtils;
import com.bql.utils.NumberUtlis;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.ui.activity.me.moneyaccount.BalanceActivity;
import com.zhilianbao.leyaogo.utils.ImageUtils;
import com.zhilianbao.leyaogo.utils.LogicCodeBlock;
import com.zhilianbao.leyaogo.utils.Utils;

import java.util.List;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 首页魔方 ItemViewProvider
 */
public class HomeAppMagicViewProvider
        extends ItemViewProvider<HomeAppMagic, HomeAppMagicViewProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_home_app_magic, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull HomeAppMagic homeAppMagic) {
        if (homeAppMagic.getData() != null && !CheckUtils.isEmpty(homeAppMagic.getData().getList()))
            holder.setMagicContent(homeAppMagic.getData().getType(), homeAppMagic.getData().getList());
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout mRlMagic;

        ViewHolder(View itemView) {
            super(itemView);
            mRlMagic = (RelativeLayout) itemView.findViewById(R.id.rl_magic_content);
        }

        private void setMagicContent(int type, List<HomeAppMagic.HomeAppMagicData.HomeAppMagicBean> list) {
            mRlMagic.removeAllViews();
            Context mContext = mRlMagic.getContext();
            mRlMagic.setLayoutParams(new LinearLayout.LayoutParams(Utils.getScreenWidth(), Utils.getScreenWidth()));
            double unit = NumberUtlis.div(Utils.getScreenWidth(), type);
            for (HomeAppMagic.HomeAppMagicData.HomeAppMagicBean homeAppMagicBean : list) {
                ImageView ivItem = new ImageView(mContext);
                RelativeLayout.LayoutParams imageLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                imageLayoutParams.width = (int) NumberUtlis.mul(homeAppMagicBean.getW(), unit);
                imageLayoutParams.height = (int) NumberUtlis.mul(homeAppMagicBean.getH(), unit);
                imageLayoutParams.leftMargin = (int) NumberUtlis.mul(homeAppMagicBean.getX(), unit);
                imageLayoutParams.topMargin = (int) NumberUtlis.mul(homeAppMagicBean.getY(), unit);
                ivItem.setLayoutParams(imageLayoutParams);
                ivItem.setScaleType(ImageView.ScaleType.FIT_XY);
                ImageUtils.loadGoodsPic(Utils.returnUrlShow(homeAppMagicBean.getImg()), ivItem);
                ivItem.setOnClickListener(v -> {
                    switch (homeAppMagicBean.getType()) {
                        //跳转到商品详情
                        case 0:
                            Utils.startGoodsDetails((Activity) mContext, (int) homeAppMagicBean.getId(), (int)homeAppMagicBean.getGoodsSkuId(), false);
                            break;
                        //跳转到商品列表
                        case 1:
                            Utils.startGoodsClassifyList((Activity) mContext, homeAppMagicBean.getId(), homeAppMagicBean.getName());
                            break;
                        //跳转到专题活动详情
                        case 2:
                        case 3:
                            Utils.startActivityDetails((Activity) mContext, homeAppMagicBean.getId(), false);
                            break;

                        //充值有礼
                        case 4:
                            if (Utils.isUserLogin(true, LogicCodeBlock.LogicState.Recharge.value)) {
                                Utils.startActivity((Activity) mContext, BalanceActivity.class);
                            } else {
                                LogicCodeBlock.create().registerLogicCodeCallback(logicState -> {
                                            if (logicState == LogicCodeBlock.LogicState.Recharge.value)
                                                Utils.startActivity((Activity) mContext, BalanceActivity.class);
                                        }
                                );
                            }

                            break;
                    }
                });
                mRlMagic.addView(ivItem);
            }
        }
    }
}